package com.bupt.service.common.impl;


import com.bupt.Factory.ProjectResultCenter;
import com.bupt.basic.IBasicNetWork;
import com.bupt.dto.service.ServiceDto;
import com.bupt.exception.BaseException;
import com.bupt.service.common.DemandService;
import com.bupt.utils.LocalVerify;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service("planResultService")
public class PlanResultService {
    @Autowired
    DemandService demandService;
   public Object getPlanResult(String pId,String serviceId) throws BaseException{
        LocalVerify.verifyString(pId, "项目的ID");
        // 获取规划的结果
        ProjectResultCenter.PlanResultData planResultData = ProjectResultCenter.getPlanResultFromFile(pId);
        if (null == planResultData) {
            throw new BaseException("暂无规划结果");
        }
        ReturnRouteResult rrr=new ReturnRouteResult();
        rrr.setWorkRoute(planResultData.getWorkRoute().get(serviceId));
        rrr.setProtectRoute(planResultData.getProtectRoute().get(serviceId));
        return rrr;
    }
    //获取规划后的端口占用情况
    public Object getPlanResultPort(String pId,String nodeId) throws BaseException{
        LocalVerify.verifyString(pId, "项目的ID");
        // 获取规划的结果
        ProjectResultCenter.PlanResultData planResultData = ProjectResultCenter.getPlanResultFromFile(pId);
        if (null == planResultData) {
            throw new BaseException("暂无规划结果");
        }
        Map<String, List<IBasicNetWork.PortL>> ports=planResultData.getPortsMap();
        return ports.get(nodeId);
    }

    //返回规划后的链路承载情况
    public Object getPlanResultService( String linkId, String pId) throws BaseException {

        LocalVerify.verifyString(linkId, "链路的ID");
        LocalVerify.verifyString(pId, "项目的ID");
        ProjectResultCenter.PlanResultData planResultData = ProjectResultCenter.getPlanResultFromFile(pId);
        //记录一条链路上承载的所有业务,接受数据
        List<ServiceDto> LT= new ArrayList<>();


        if (null != planResultData) {
            // 获取链路上的信息
            ProjectResultCenter.LinkMsg linkMsg = planResultData.getLinkMsg().get(linkId);
            if (null == linkMsg) {
                return null;
            }
            //获取业务信息
         List<ServiceDto> demands= (List<ServiceDto>)demandService.getDemandsList(pId);
            //向链路上添加业务
            for ( String serviceId : linkMsg.getServiceIdSet()){
                for(ServiceDto demand:demands){
                    if(serviceId.equals(demand.getId())){
                        LT.add(demand);
                    }
                }
            }
            // 如果不存在 就会 直接返回

            return LT;
        }
        return null;
    }


    // 需要返回的业务的工作路由的情况
    @Getter
    @Setter
    private static class ReturnRouteResult {
        ProjectResultCenter.RouteType workRoute;
        ProjectResultCenter.RouteType protectRoute;
    }




    }

